/**
 * 
 */
package com.hydra.designpattern.observer;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @Created on 2013-4-2
 * @Title:
 * @Description:
 * @author:黄海军
 */
public class Person implements Subject, Observer {
	private String name = "";

	/**
	 * <p>
	 * Discription:[方法功能中文描述]
	 * </p>
	 * 
	 * @return String name.
	 */
	public String getName() {
		return name;
	}

	/**
	 * <p>
	 * Discription:[方法功能中文描述]
	 * </p>
	 * 
	 * @param name
	 *            The name to set.
	 */
	public void setName(String name) {
		this.name = name;
	}

	private int status = 0;

	/**
	 * <p>
	 * Discription:[方法功能中文描述]
	 * </p>
	 * 
	 * @return int status.
	 */
	public int getStatus() {
		return status;
	}

	/**
	 * <p>
	 * Discription:[方法功能中文描述]
	 * </p>
	 * 
	 * @param status
	 *            The status to set.
	 */
	public void setStatus(int status) {
		this.status = status;
		if (hasChaged()) {
			notifyObservers();
		}
	}

	private boolean isChanged = false;
	private List<Observer> observers;

	/**
	 * Created on 2013-4-2
	 * <p>
	 * Discription:[方法功能中文描述]
	 * </p>
	 * 
	 * @author:[hydrahuang]
	 * @update:[日期YYYY-MM-DD] [更改人姓名]
	 */
	public void update(Person person) {
		System.out.println("姓名:" + name);
		System.out.println("好友:" + person.getName() + "的状态已经改变，现在是"
				+ person.getStatus());
	}

	/**
	 * <p>
	 * Discription:[构造器方法描述]
	 * </p>
	 * 
	 * @coustructor 方法.
	 */
	public Person(String name) {
		super();
		this.name = name;
		observers = new ArrayList<Observer>();
	}

	/**
	 * Created on 2013-4-2
	 * <p>
	 * Discription:[方法功能中文描述]
	 * </p>
	 * 
	 * @author:[hydrahuang]
	 * @update:[日期YYYY-MM-DD] [更改人姓名]
	 */
	public void addObserver(Observer o) {
		observers.add(o);

	}

	/**
	 * Created on 2013-4-2
	 * <p>
	 * Discription:[方法功能中文描述]
	 * </p>
	 * 
	 * @author:[hydrahuang]
	 * @update:[日期YYYY-MM-DD] [更改人姓名]
	 */
	public void deleteObserver(Observer o) {
		observers.remove(o);

	}

	/**
	 * Created on 2013-4-2
	 * <p>
	 * Discription:[方法功能中文描述]
	 * </p>
	 * 
	 * @author:[hydrahuang]
	 * @update:[日期YYYY-MM-DD] [更改人姓名]
	 */
	public void notifyObservers() {
		for (Observer o : observers) {
			o.update(this);
		}

	}

	/**
	 * Created on 2013-4-2
	 * <p>
	 * Discription:[方法功能中文描述]
	 * </p>
	 * 
	 * @author:[hydrahuang]
	 * @update:[日期YYYY-MM-DD] [更改人姓名]
	 */
	public boolean hasChaged() {
		if (this.getStatus() == 1) {
			return true;
		}
		return false;
	}

}
